home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-02-19 | 17.1 KB | 485 lines | [TEXT/CWIE] |
-
- #include "FSSpecification.h"
- #include "MoreStrings.h"
- #include "MoreAEM.h"
- #include "AbstractData.h"
-
- //
- // For 'EqualString'
- //
- #include <TextUtils.h>
-
- //========================================================================================
- // CLASS TFSSpecification
- //========================================================================================
-
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::TFSSpecification:
- //----------------------------------------------------------------------------------------
- TFSSpecification::TFSSpecification()
- {
- fFileSpec.vRefNum = 0;
- fFileSpec.parID = 0;
- } // TFSSpecification::TFSSpecification
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::TFSSpecification:
- //----------------------------------------------------------------------------------------
- TFSSpecification::TFSSpecification(const FSSpec& fromSpecification)
- {
- fFileSpec = fromSpecification;
- } // TFSSpecification::TFSSpecification
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::TFSSpecification:
- //----------------------------------------------------------------------------------------
- TFSSpecification::TFSSpecification(const TFSSpecification& fromSpecification)
- {
- fFileSpec = fromSpecification;
- } // TFSSpecification::TFSSpecification
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::TFSSpecification:
- //----------------------------------------------------------------------------------------
- TFSSpecification::TFSSpecification(const TDescriptor& fromDesc, Boolean forceCreate /*= false*/, OSType creator /*= 0*/, OSType fileType /*= 0*/)
- {
- //
- // We should have coersion handlers to go from typeChar (a pathname) to typeFSS
- //
- if(fromDesc.DescriptorType() == typeChar)
- {
- TString fileSpecName(fFileSpec.name);
-
- fFileSpec.vRefNum = 0;
- fFileSpec.parID = 0;
- fileSpecName = fromDesc;
- }
- else
- fromDesc.GetDescriptorData(TUpdataDataReference(typeFSS, (Ptr)&fFileSpec, sizeof(FSSpec), sizeof(FSSpec)));
-
- //
- // Make sure that the fileSpec is valid
- //
- if(forceCreate)
- {
- FSpCreate(&fFileSpec, creator, fileType, 0);
- }
- FSMakeFSSpec(fFileSpec.vRefNum, fFileSpec.parID, fFileSpec.name, &fFileSpec);
- } // TFSSpecification::TFSSpecification
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::TFSSpecification:
- //
- // This finds the applicatio file that the given psn was launched from
- //----------------------------------------------------------------------------------------
- TFSSpecification::TFSSpecification(const ProcessSerialNumber& psn)
- {
- ProcessInfoRec info;
- info.processInfoLength = sizeof(ProcessInfoRec);
- info.processName = nil;
- info.processAppSpec = &fFileSpec;
- info.processLocation = nil;
- GetProcessInformation(&psn, &info);
- } // TFSSpecification::TFSSpecification
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::~TFSSpecification
- //----------------------------------------------------------------------------------------
- TFSSpecification::~TFSSpecification()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::operator=:
- //----------------------------------------------------------------------------------------
- const TFSSpecification& TFSSpecification::operator=(const TFSSpecification& rhs)
- {
- fFileSpec = rhs;
-
- return *this;
- } // TFSSpecification::operator=
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::operator==:
- //----------------------------------------------------------------------------------------
- Boolean TFSSpecification::operator==(const TFSSpecification& rhs) const
- {
- FSSpec compareFSSpec = rhs;
-
- return ( (fFileSpec.vRefNum == compareFSSpec.vRefNum) &&
- (fFileSpec.parID == compareFSSpec.parID) &&
- (EqualString(fFileSpec.name, compareFSSpec.name, false, true)));
- } // TFSSpecification::operator==
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::Open:
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::Open(OpenFileRefNum& refNum, SInt8 permission) const
- {
- if(permission == kDefaultPerm)
- permission = fsRdWrPerm;
- return FSpOpenDF(&fFileSpec, permission, &refNum);
- } // TFSSpecification::Open
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::OpenResFile:
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::OpenResFile(OpenResFileRefNum& refNum, SInt32 permission) const
- {
- if(permission == kDefaultPerm)
- permission = fsRdPerm;
- refNum = FSpOpenResFile(&fFileSpec, permission);
- return ResError();
- } // TFSSpecification::Open
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::Delete:
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::Delete()
- {
- return FSpDelete(&fFileSpec);
- } // TFSSpecification::Delete
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::Move:
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::Move(TFSSpecification& destinationOfMove)
- {
- FSSpec fileSpecOfDestinationSpec = destinationOfMove;
-
- return FSpCatMove(&fFileSpec, &fileSpecOfDestinationSpec);
- } // TFSSpecification::Move
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::Name:
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::Name(TString& fileName) const
- {
- const TString fileSpecName((unsigned char*)fFileSpec.name);
- fileName = fileSpecName;
-
- // CopyMemory(&fFileSpec.name[0], &fileName[0], fFileSpec.name[0] + 1);
-
- return noErr;
- } // TFSSpecification::Name
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::operator FSSpec
- //----------------------------------------------------------------------------------------
- TFSSpecification::operator FSSpec() const
- {
- return fFileSpec;
- } // TFSSpecification::operator FSSpec
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::Rename:
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::Rename(const TString& newName)
- {
- return FSpRename(&fFileSpec, newName);
- } // TFSSpecification::Rename
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::ExchangeFiles:
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::ExchangeFiles(TFSSpecification& exchangeWith)
- {
- FSSpec fileSpecOfExchangeFile = exchangeWith;
-
- return FSpExchangeFiles(&fFileSpec, &fileSpecOfExchangeFile);
- } // TFSSpecification::ExchangeFiles
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::GetFinderInfo:
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::GetFinderInfo(FInfo& fndrInfo) const
- {
- return FSpGetFInfo(&fFileSpec, &fndrInfo);
- } // TFSSpecification::GetFinderInfo
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::SetFinderInfo:
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::SetFinderInfo(const FInfo& fndrInfo) const
- {
- return FSpSetFInfo(&fFileSpec, &fndrInfo);
- } // TFSSpecification::SetFinderInfo
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::CreateFile:
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::CreateFile(OSType creator, OSType fileType, ScriptCode scriptTag /* = 0*/)
- {
- OSStatus err = FSpCreate(&fFileSpec, creator, fileType, scriptTag);
-
- //
- // 'fileSpec' may contain a relative pathname; call 'FSMakeFSSpec'
- // to make an FSSpec whose name field contains only the name
- // of the file, and none of the path to that file.
- //
- if(err == noErr)
- err = FSMakeFSSpec(fFileSpec.vRefNum, fFileSpec.parID, fFileSpec.name, &fFileSpec);
-
- return err;
- } // TFSSpecification::CreateFile
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::CreateNewChildFile:
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::CreateNewChildFile(const TString& newName, OSType creator, OSType fileType, TFSSpecification& newFile) const
- {
- SInt32 dirID = 0;
- OSStatus err = noErr;
-
- err = this->GetDirID(dirID);
- if(err == noErr)
- err = newFile.MakeFSSpec(this->VRefNum(), dirID, newName);
- if(err == noErr)
- err = newFile.CreateFile(creator, fileType);
-
- return err;
- } // TFSSpecification::CreateNewChildFile
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::CreateNewChildFolder:
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::CreateNewChildFolder(const TString& newName, TFSSpecification& newFolder) const
- {
- TFSSpecification newFolderSpec;
- SInt32 dirID;
- OSStatus err = noErr;
-
- err = this->GetDirID(dirID);
- if(err == noErr)
- err = newFolderSpec.MakeFSSpec(this->VRefNum(), dirID, newName);
- if(err == noErr)
- {
- FSSpec fsSpec = newFolderSpec;
- SInt32 createdDirID;
- err = FSpDirCreate(&fsSpec, 0, &createdDirID);
- newFolder.AdoptSpec(this->VRefNum(), createdDirID);
- }
-
- return err;
- } // TFSSpecification::CreateNewChildFolder
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::ContainerSpecification
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::ContainerSpecification(TFSSpecification& container) const
- {
- OSStatus err = noErr;
-
- if(this->ParentDirID() == fsRtParID)
- err = fnfErr;
- else if(this->ParentDirID() == fsRtDirID)
- err = this->VolumeSpecification(container);
- else
- err = container.AdoptSpec(this->VRefNum(), this->ParentDirID());
-
- return err;
- }
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::VolumeSpecification
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::VolumeSpecification(TFSSpecification& volumeObjectSpec) const
- {
- FSSpec rootSpec;
-
- //
- // Is this the best thing to do, or should we call volumeObjectSpec->AdoptSpec(this->VRefNum(), fsRtDirID)?
- //
- rootSpec.vRefNum = this->VRefNum();
- rootSpec.parID = fsRtParID;
- rootSpec.name[0] = 0;
-
- volumeObjectSpec = TFSSpecification(rootSpec);
-
- return noErr;
- }
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::AdoptSpec
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::AdoptSpec(SInt16 vRefNum, SInt32 dirID)
- {
- CInfoPBRec pb;
-
- //
- // Set input fields; ioFDirIndex == -1 means look up
- // by vRefNum and dirID (0 == look up by name, positive
- // numbers == look up n'th child)
- //
- pb.dirInfo.ioFDirIndex = -1;
- pb.dirInfo.ioVRefNum = vRefNum;
- pb.dirInfo.ioDrDirID = dirID;
-
- //
- // Set output fields
- //
- pb.dirInfo.ioNamePtr = fFileSpec.name;
-
- //
- // Set fields needed to make call work correctly
- // (e.g. ioCompletion) and make the GetCatInfo call.
- //
- pb.dirInfo.ioResult = noErr;
- pb.dirInfo.ioCompletion = nil;
- pb.dirInfo.ioFVersNum = 0;
- PBGetCatInfoSync(&pb);
- OSStatus err = pb.dirInfo.ioResult;
-
- if(err == noErr)
- {
- //
- // Copy the results into our result FSSpec
- // (n.b. name was already filled in by
- // GetCatInfo)
- //
- fFileSpec.vRefNum = vRefNum;
- fFileSpec.parID = pb.dirInfo.ioDrDirID;
- }
-
- return err;
- }
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::GetDirID
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::GetDirID(SInt32& dirID) const
- {
- Str255 name;
- CInfoPBRec pb;
-
- //
- // Set input fields; ioFDirIndex == -1 means look up
- // by vRefNum and dirID (0 == look up by name, positive
- // numbers == look up n'th child)
- //
- pb.dirInfo.ioFDirIndex = 0;
- pb.dirInfo.ioVRefNum = this->VRefNum();
- pb.dirInfo.ioDrDirID = this->ParentDirID();
- CopyMemory(fFileSpec.name, name, fFileSpec.name[0] + 1);
- pb.dirInfo.ioNamePtr = name;
-
- //
- // Set fields needed to make call work correctly
- // (e.g. ioCompletion) and make the GetCatInfo call.
- //
- pb.dirInfo.ioResult = noErr;
- pb.dirInfo.ioCompletion = nil;
- pb.dirInfo.ioFVersNum = 0;
- PBGetCatInfoSync(&pb);
- OSStatus err = pb.dirInfo.ioResult;
-
- if(err == noErr)
- {
- dirID = pb.dirInfo.ioDrDirID;
- }
-
- return err;
- }
-
- //----------------------------------------------------------------------------------------
- // TFSSpecification::MakeFSSpec
- //----------------------------------------------------------------------------------------
- OSStatus TFSSpecification::MakeFSSpec(SInt16 vRefNum, SInt32 dirID, ConstStr255Param name)
- {
- FSSpec newSpec;
-
- CopyMemory(name, newSpec.name, name[0] + 1);
- newSpec.vRefNum = vRefNum;
- newSpec.parID = dirID;
-
- return noErr;
- }
-
- //========================================================================================
- // CLASS TOpenFileRefNum
- //========================================================================================
-
-
- //----------------------------------------------------------------------------------------
- // TOpenFileRefNum::Open:
- //----------------------------------------------------------------------------------------
- OSStatus TOpenFileRefNum::Open(const TFSSpecification& fileToOpen, SInt8 permission)
- {
- OSStatus err = fileToOpen.Open(fRefNum, permission);
- fIsOpen = (err == noErr);
-
- return err;
- } // TOpenFileRefNum::Open
-
- //----------------------------------------------------------------------------------------
- // TOpenFileRefNum::Close:
- //----------------------------------------------------------------------------------------
- OSStatus TOpenFileRefNum::Close()
- {
- OSStatus err = FSClose(fRefNum);
- fRefNum = kInvalidOpenRefNum;
- fIsOpen = false;
-
- return err;
- } // TOpenFileRefNum::Close
-
- //----------------------------------------------------------------------------------------
- // TOpenFileRefNum::Read:
- //----------------------------------------------------------------------------------------
- OSStatus TOpenFileRefNum::Read(SInt32 *count, void *buffPtr)
- {
- return FSRead(fRefNum, count, buffPtr);
- } // TOpenFileRefNum::Read
-
- //----------------------------------------------------------------------------------------
- // TOpenFileRefNum::Write:
- //----------------------------------------------------------------------------------------
- OSStatus TOpenFileRefNum::Write(SInt32 *count, void *buffPtr)
- {
- return FSWrite(fRefNum, count, buffPtr);
- } // TOpenFileRefNum::Write
-
- //----------------------------------------------------------------------------------------
- // TOpenFileRefNum::GetEndOfFile:
- //----------------------------------------------------------------------------------------
- OSStatus TOpenFileRefNum::GetEndOfFile(SInt32* logEOF)
- {
- return GetEOF(fRefNum, logEOF);
- } // TOpenFileRefNum::GetEndOfFile
-
- //----------------------------------------------------------------------------------------
- // TOpenFileRefNum::SetEndOfFile:
- //----------------------------------------------------------------------------------------
- OSStatus TOpenFileRefNum::SetEndOfFile(SInt32 logEOF)
- {
- return SetEOF(fRefNum, logEOF);
- } // TOpenFileRefNum::SetEndOfFile
-
- //----------------------------------------------------------------------------------------
- // TOpenFileRefNum::GetFilePosition:
- //----------------------------------------------------------------------------------------
- OSStatus TOpenFileRefNum::GetFilePosition(SInt32* filePos)
- {
- return GetFPos(fRefNum, filePos);
- } // TOpenFileRefNum::GetFilePosition
-
- //----------------------------------------------------------------------------------------
- // TOpenFileRefNum::SetFilePosition:
- //----------------------------------------------------------------------------------------
- OSStatus TOpenFileRefNum::SetFilePosition(SInt16 posMode, SInt32 posOff)
- {
- return SetFPos(fRefNum, posMode, posOff);
- } // TOpenFileRefNum::SetFilePosition
-
- //----------------------------------------------------------------------------------------
- // TOpenFileRefNum::SetFilePosition:
- //----------------------------------------------------------------------------------------
- OSStatus TOpenFileRefNum::SetFilePosition(SInt32 posOff)
- {
- return this->SetFilePosition(fsFromStart, posOff);
- } // TOpenFileRefNum::SetFilePosition
-
-
-
-